package sz.szsmk.citizencard.widget;

import android.animation.ArgbEvaluator;
import android.content.Context;
import android.graphics.Color;

import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.content.ContextCompat;
import androidx.appcompat.widget.Toolbar;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import sz.szsmk.citizencard.R;

/**
 * author : hyt
 * date : 2019/11/13
 * description :
 */
public class TranslucentBehavior  extends CoordinatorLayout.Behavior<Toolbar> {

    /**标题栏的高度*/
    private int mToolbarHeight = 0;

    private Context context;

    public TranslucentBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, Toolbar child, View dependency) {
        return dependency instanceof TextView;
    }

    /**
     * 必须要加上  layout_anchor，对方也要layout_collapseMode才能使用
     */
    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, Toolbar child, View dependency) {

        // 初始化高度
        if (mToolbarHeight == 0) {
            //为了更慢的
            mToolbarHeight = child.getBottom() * 2;
        }

        //计算toolbar从开始移动到最后的百分比
        float percent = dependency.getY() / mToolbarHeight;

        //百分大于1，直接赋值为1
        if (percent >= 1) {
            percent = 1f;
        }

        // 计算alpha通道值
        float alpha = percent * 255;
//        Log.i("percent",""+percent+"<>"+alpha);
        //设置背景颜色
        child.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255));

        RelativeLayout rl_content= child.findViewById(R.id.rl_content);
        TextView tv_title = rl_content.findViewById(R.id.tv_title);
        TextView tv_transactionRecord = rl_content.findViewById(R.id.tv_transactionRecord);

        int PAGE_COLOR_ONE = ContextCompat.getColor(this.context, R.color.white);
        int PAGE_COLOR_TWO = ContextCompat.getColor(this.context, R.color.white);
        ArgbEvaluator argbEvaluator = new ArgbEvaluator();

        int currentLastColor = (int) (argbEvaluator.evaluate(percent, PAGE_COLOR_ONE, PAGE_COLOR_TWO));
        tv_title.setTextColor(currentLastColor);
        tv_transactionRecord.setTextColor(currentLastColor);

        return true;
    }
}